package handler

import (
	"github.com/gin-gonic/gin"
	"post-master/model"
)

func Add(c *gin.Context) {
	title := c.PostForm("title")
	content := c.PostForm("content")
	picture := c.PostForm("picture")
	if title == "" || content == "" || picture == "" {
		c.JSON(200, gin.H{
			"msg":  "请将数据填写完整",
			"code": 400,
		})
		return
	}
	var article model.Article
	article.Title = title
	article.Content = content
	article.Picture = picture
	err := model.DB.Create(&article).Error
	if err != nil {
		c.JSON(200, gin.H{
			"msg":  "添加失败",
			"code": 400,
		})
		return
	}
	c.JSON(200, gin.H{
		"msg":  "添加成功",
		"code": 200,
	})
}
func List(c *gin.Context) {
	var article []model.Article
	if err := model.DB.Debug().Find(&article).Error; err != nil {
		c.JSON(200, gin.H{
			"msg":  "展示失败",
			"code": 400,
		})
		return
	}
	c.JSON(200, gin.H{
		"msg":  "展示成功",
		"code": 200,
		"list": article,
	})
}
